package com.thirteen.app.service.impl;

import com.thirteen.app.daoTest.*;
import com.thirteen.app.dto.JudgeDto;
import com.thirteen.app.dto.JudgeSubjectiveResquest.JudgeByPersonDto;
import com.thirteen.app.entity.ExamRecord;
import com.thirteen.app.entity.Question;
import com.thirteen.app.entity.QuestionType;
import com.thirteen.app.service.JudgeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("judgeService")
public class JudgeServiceImpl implements JudgeService {

    @Resource
    ExamRecordDao examRecordDao;

    @Resource
    QuestionTypeDao questionTypeDao;

    @Resource
    QuestionDao questionDao;

    @Resource
    QuestionTypeQuestionDao questionTypeQuestionDao;

    @Resource
    JudgeDao judgeDao;

    @Resource
    PaperObjectiveDao paperObjectiveDao;

    //    人工阅卷
    @Override
    public void judgeByPerson(JudgeByPersonDto judgeByPersonDto) {
        judgeDao.insertJudgeByPerson(judgeByPersonDto);
        //错题插入位置
    }

    //    机器阅卷
    @Override
    public Integer judgeByMachine(Integer examRecordId) {
        //判断客观题答案
        Integer totalScore = 0;
        ExamRecord examRecord = examRecordDao.queryExamRecord(examRecordId);
        List<QuestionType> questionIdList = questionTypeDao.queryQuestionTypeObjective(examRecord.getPaperId());
        for (QuestionType i : questionIdList) {
            List<Question> questionList = questionDao.queryQuestion(i.getQuestionTypeId());
            for (Question j : questionList) {
                JudgeDto judgeDto = new JudgeDto();
                judgeDto.setExamRecordId(examRecordId);
                judgeDto.setQuestionId(j.getQuestionId());
                Integer actuallScore = 0;
                Boolean isRight = false;
                Integer questionId = j.getQuestionId();
                Integer questionTypeId = i.getQuestionTypeId();
                switch (j.getQuestionType()) {
                    case 1:
                    case 3:
                        if (paperObjectiveDao.judgeSingleAndJudge(examRecordId, questionId) == 1) {
                            actuallScore = questionTypeQuestionDao.queryQuestionScore(questionId, questionTypeId);
                            isRight = true;
                        } else {
                            //错题插入位置
                        }
                        break;
                    case 2:
                        if (paperObjectiveDao.judgeMultiHaveWrong(examRecordId, questionId) == 0 && paperObjectiveDao.queryMultiChecked(examRecordId, questionId) > 0) {
                            System.out.println(paperObjectiveDao.queryMultiRightNum(examRecordId, questionId));
                            System.out.println(paperObjectiveDao.queryMultiCheckedRightNum(examRecordId, questionId));
                            if (paperObjectiveDao.queryMultiRightNum(examRecordId, questionId) == paperObjectiveDao.queryMultiCheckedRightNum(examRecordId, questionId)) {
                                actuallScore = questionTypeQuestionDao.queryQuestionScore(questionId, questionTypeId);
                                isRight = true;
                            } else {
                                actuallScore = (questionTypeQuestionDao.queryQuestionScore(questionId, questionTypeId) + 2) / 2;
                            }
                        } else {
                            //错题插入位置
                        }
                        break;
                }
                totalScore += actuallScore;
                judgeDto.setActualScore(actuallScore);
                judgeDto.setIsRight(isRight);
                judgeDao.insertJudgeByMachine(judgeDto);
            }
        }
        return totalScore;
    }


}
